﻿using Pb;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RegisterModel : MonoBehaviour
{
    //输入框
    public UIInput PhoneNum;
    public UIInput Passward;
    public UIInput UserName;
    public UIInput YanZhengMa;
    //按钮
    public GameObject RegisteBtn;
    public GameObject ReturnBtn;
    public GameObject VerificationCodeBtn;
   
    //预设物
    //public GameObject Login;
    public GameObject Register;

    EginUser user = EginUser.Instance;

    public UISprite sprite;
    public UILabel label;
    void Start()
    {
        UIEventListener.Get(RegisteBtn).onClick = RegisterBtnClick;
        UIEventListener.Get(ReturnBtn).onClick = RegisterBtnClick;
        UIEventListener.Get(VerificationCodeBtn).onClick = RegisterBtnClick;

        sprite = VerificationCodeBtn.GetComponent<UISprite>();
        label = VerificationCodeBtn.transform.GetChild(0).GetComponent<UILabel>();
    }
    /// <summary>
    /// 点击注册按钮
    /// </summary>
    void RegisterBtnClick(GameObject Obj)
    {
        string username = ProtoDataToMsg.stringToUTF8(PhoneNum.value);
      
        string passwd = ProtoDataToMsg.stringToUTF8(DataSet.StringToMd5(Passward.value));
        string again_Passward = ProtoDataToMsg.stringToUTF8(DataSet.StringToMd5(Passward.value));
        if (Obj.name == "RegisterBtn")//判断按钮名字
        {
            if (PhoneNum.value.Length != 0 && Passward.value.Length != 0 && UserName.value.Length != 0 && YanZhengMa.value.Length != 0)
            {
                print("进入了");
                RegisterRequest re = new Pb.RegisterRequest { Username = username, Passwd = passwd, Nickname = UserName.value, ValidCode = int.Parse(YanZhengMa.value) };
                SocketManager socketManager = SocketManager.Instance;
                socketManager.SendProtobuf1(re, MsgId.RegisterRequest);

            }
            else
            {
                user.logMsg = "请输入完整信息";
                GameObject Set = NGUITools.AddChild(gameObject, (GameObject)(Resources.Load("LogMsg")));
            }

        }
        else if (Obj.name == "ReturnBtn")
        {
            Destroy(gameObject);
          //  Register.SetActive(false);
        }
        else if (Obj.name == "VerificationCodeBtn")
        {
            //请求验证码
            if (PhoneNum.value.Length != 0)
            {
                //请求验证码
                TelephoneCodeRequest lg = new TelephoneCodeRequest { Telephone = PhoneNum.value, CodeType = TelephoneCodeType.Register };
                SocketManager socketManager = SocketManager.Instance;
                socketManager.SendProtobuf1(lg, MsgId.TelephoneCodeRequest);
               // CountDown("yzm2", true);
            }
            else
            {
                user.logMsg = "请输入完整信息";
                GameObject Set = NGUITools.AddChild(gameObject, (GameObject)(Resources.Load("LogMsg")));
            }
        }
    }
    /// <summary>
    /// 倒计时
    /// </summary>
    /// <param name="time"></param>
    /// <param name="name"></param>
    void CountDown(string name, bool isShow)
    {
        sprite.spriteName = name;
        if (isShow)
        {
            label.gameObject.SetActive(true);
            VerificationCodeBtn.GetComponent<BoxCollider>().enabled = false;
            StartCoroutine(reduceTime(60, label));
        }
        else
        {
            VerificationCodeBtn.GetComponent<BoxCollider>().enabled = true;
            label.gameObject.SetActive(false);
        }
    }
    IEnumerator reduceTime(int time, UILabel label)
    {
        label.text = time.ToString();
        while (time > 0)
        {
            yield return new WaitForSeconds(1);
            time--;
            if (time <= 0)
            {
                CountDown("yzm1", false);
                yield break;
            }
            label.text = time.ToString();
        }
    }
}
